import 'package:appflutterhomeschool/api/api_service.dart';
import 'package:appflutterhomeschool/api/dio_utils.dart';
import 'package:appflutterhomeschool/common/common.dart';
import 'package:appflutterhomeschool/model/form_switch_model.dart';
import 'package:appflutterhomeschool/model/student_information_model.dart';
import 'package:appflutterhomeschool/ui/dialogs/prompt_dialog_widgets.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import '../my_app_view_model.dart';

///用户： ninetailedfox
///时间： 2020/6/26
///邮箱： 1037438704@qq.com
///功能： 学生信息状态管理页面
class StudentInformationViewModel extends ChangeNotifier {
  List<StudentInformationModel> studentList = [];

  List<FormSwitchModel> gridList = [
    FormSwitchModel("爸爸",
        imageUrl: "baba", judge: true, imageUrlFalse: "baba_false"),
    FormSwitchModel("妈妈",
        imageUrl: "mama", judge: false, imageUrlFalse: "mama_false"),
    FormSwitchModel("爷爷",
        imageUrl: "yeye", judge: false, imageUrlFalse: "yeye_false"),
    FormSwitchModel("奶奶",
        imageUrl: "nainai", judge: false, imageUrlFalse: "nainai_false")
  ];

  BuildContext me;

  bool identity;

  StudentInformationViewModel(this.me);

  initView() {
    studentList.clear();
    MyAppViewModel myAppViewModel = Provider.of<MyAppViewModel>(me);
    //true 是老师  false 是家长
    identity = myAppViewModel.identity;
    if (identity) {
      DioUtils.getInstance()
          .post(ApiService.getSubjects, {"token": Constant.getToken()},
              (successCallBack) {
        gridList.clear();

        (successCallBack["data"]["subject_list"] as List).forEach((element) {
          DioUtils.getInstance()
              .post(ApiService.getSubjectIcon, {"id": element["id"]},
                  (successCallBack) {
            if (successCallBack["data"]["name"] == "") {
              return;
            }
            gridList.add(FormSwitchModel(element["name"],
                imageUrl: successCallBack["data"]["subject_icon"]["icon"] ?? "",
                imageUrlFalse:
                    successCallBack["data"]["subject_icon"]["false_icon"] ?? "",
                judge: false,
                fileAndUrl: 1));
          }, (errorCallBack) {});
        });
        notifyListeners();
      }, (errorCallBack) {});
      //获取班级列表
      DioUtils.getInstance().post(ApiService.getClasses, {
        "token": Constant.getToken(),
      }, (successCallBack) {
        (successCallBack["data"]["class_list"] as List).forEach((element) {
          studentList.add(StudentInformationModel.fromJson(identity, element));
        });
        for (int i = 0; i < studentList.length; i++) {
          print("===========studentList==========${studentList[i]?.subjectId}");
          DioUtils.getInstance().post(
              ApiService.getSubjectIcon, {"id": studentList[i]?.subjectId},
              (successCallBack) {
            if (successCallBack["data"] == null) {
              return;
            }
            studentList[i].imageUrl =
                successCallBack["data"]["subject_icon"]["icon"];
            studentList[i].imageUrlFalse =
                successCallBack["data"]["subject_icon"]["false_icon"];
          }, (errorCallBack) {});
        }
        notifyListeners();
      }, (errorCallBack) {
        print("errorCallBack ======== $errorCallBack");
      });
    } else {
      //获取学生列表
      DioUtils.getInstance().post(ApiService.getStudents, {
        "token": Constant.getToken(),
      }, (successCallBack) {
        (successCallBack["data"]["students"] as List).forEach((element) {
          studentList.add(StudentInformationModel.fromJson(identity, element));
        });
        for (int i = 0; i < studentList.length; i++) {
          DioUtils.getInstance().post(
              ApiService.getSubjectIcon, {"id": studentList[i]?.subjectId},
              (successCallBack) {
            if (successCallBack["data"] == null) {
              return;
            }
            studentList[i].imageUrl =
                successCallBack["data"]["subject_icon"]["icon"];
            studentList[i].imageUrlFalse =
                successCallBack["data"]["subject_icon"]["false_icon"];
          }, (errorCallBack) {});
        }
        notifyListeners();
      }, (errorCallBack) {
        print("errorCallBack ======== $errorCallBack");
      });
    }
  }

  deleteOnTap(int index) {
    PromptDialogWidgets(me).showPromptDialog(
        title: "提示",
        content: "是否删除",
        cencel: "否",
        sure: "是",
        sureOnTap: () {
          DioUtils.getInstance().post(
              identity ? ApiService.deleteCls : ApiService.delStudent,
              identity
                  ? {
                      "token": Constant.getToken(),
                      "cls_mapping_id": studentList[index].classId
                    }
                  : {
                      "token": Constant.getToken(),
                      "student_id": studentList[index].subjectId
                    }, (successCallBack) {
            Fluttertoast.showToast(msg: successCallBack["message"]);
            initView();
          }, (errorCallBack) {});
        });
  }
}
//CLASSCODE27
